import sys
from collections import defaultdict
from pathlib import Path
# python /home/zhengzhiqiang/workflow/cnv_stat.py  /home/zhengzhiqiang/0528.S200040707.csv /data/share/S200040707/L01/SequenceStat.txt /home/zhengzhiqiang/S200040707

# 根据simplesheet和Sequence.txt生成/fastq/cnv_stat.txt
workdir=sys.argv[3]
with open(sys.argv[1])as csv,open(sys.argv[2]) as stat,open(f'{workdir}/fastq/cnv_stat.txt','w')as f2:
    _samples={}
    flag=0
    s=defaultdict(int)
    f2.write("index\treadsNumber\n")
    for i in csv:
        if flag == 0:
            if i.startswith('Sample_ID'):
                flag = 1
                continue
            continue
        if flag == 1:
            x = i.split(',')
            if x[0].find('BY')>-1:
                b=f"Barcode{x[4]}"
                _samples[b] = x[0]
    for k in stat:
        if k.find('#Sequence')>-1:continue
        k=k.strip().split('\t')
        k[1]=k[1].strip()
        s[k[1]]+=int(k[2])
   
    for m,n in s.items():
        if m in _samples:
            f2.write(f"{m}\t{n}\n")
